package com.xayy.health.mgt.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.kawins.base.result.ResultBody;
import cn.kawins.mybatis.base.BaseController;
import cn.kawins.mybatis.base.BaseModel;
import com.alibaba.fastjson.JSONObject;
import com.xayy.health.core.enums.RoleEnum;
import com.xayy.health.mgt.model.vo.RoleReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 权限查看
 *
 * @author Shichenlei
 */
@RestController
@RequestMapping(value = "/role")
@Api(tags = "角色列表")
public class RoleController extends BaseController<BaseModel<Integer>> {


    @Value("${menhu.permissionsURL}")
    private String menHuPermissionsURL;

    @Value("${plat.permissionsURL}")
    private String platPermissionsURL;


    /**
     * 查看权限信息
     */
    @ApiOperation(value = "查看权限")
    @PostMapping("view")
    public ResultBody getRole(@RequestBody RoleReqVO roleReqVO) {
        if (StrUtil.isBlank(roleReqVO.getLoginSource()) || StrUtil.isBlank(roleReqVO.getToken())) {
            throw new RuntimeException("getRole 接口参数不合规");
        }
        String loginSource = roleReqVO.getLoginSource();
        System.out.println(loginSource + "  token:" + roleReqVO.getToken());

        String canModify;
        if (Objects.equals(loginSource, "portal")) {
            canModify = getTokenPermissionByMenHu(roleReqVO.getToken());
        } else {
            canModify = getTokenPermissionByPlat2(roleReqVO.getToken());
        }

        Integer roleEnumCode = RoleEnum.READ.getCode();
        if (Objects.equals(canModify, "1")) {
            roleEnumCode = RoleEnum.ALL.getCode();
        }
        return ResultBody.success(roleEnumCode);
    }

    private String getTokenPermissionByMenHu(String token) {
        String fullUrl = menHuPermissionsURL + "?token=" + token;
        HttpRequest get = HttpUtil.createGet(fullUrl);
        String result = "0";
        try (HttpResponse response = get.execute()) {

            String body = response.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            log.info("门户权限认证结果 token:{} rsp:{}", token, jsonObject.toJSONString());
            result = (String) jsonObject.get("can_modify");
            log.info("getTokenPermissionByMenHu result:{}", result);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    private String getTokenPermissionByPlat2(String token) {
        String result = "0";
        String fullUrl = platPermissionsURL + "?token=" + token;
        HttpRequest get = HttpUtil.createGet(fullUrl);
        try (HttpResponse response = get.execute()) {
            String body = response.body();
            JSONObject jsonObject = JSONObject.parseObject(body);
            log.info("平台2.0权限认证结果 token:{} rsp:{}", token, jsonObject.toJSONString());
            result = (String) jsonObject.get("postLevel");
            log.info("getTokenPermissionByPlat2 result:{}", result);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }
}
